package com.core.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;


import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.dao.impl.DBUtil;
import com.core.dao.impl.UsernameDAOImpl;
import com.core.entities.Username;
import com.core.gui.helper.UserInfo;
import com.core.gui.main.MainFrame;
import com.core.service.UsernamSrv;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.util.List;

public class LoginDialog extends JDialog {
//	ChangePasswordDialog changePasswordDialog;

	private Connection conn;
	private final JPanel contentPanel = new JPanel();
	private final JLabel lblUsername = new JLabel("Username: ");
	public final JTextField usernameTextField = new JTextField();
	private final JLabel lblPassword = new JLabel("Password :");
	private final JPasswordField passwordField = new JPasswordField();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		try {
			LoginDialog dialog = new LoginDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		usernameTextField.setBounds(69, 8, 145, 20);
		usernameTextField.setColumns(10);
		setBounds(100, 100, 325, 145);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblUsername.setBounds(10, 11, 55, 14);

		contentPanel.add(lblUsername);

		contentPanel.add(usernameTextField);
		lblPassword.setBounds(10, 36, 53, 14);

		contentPanel.add(lblPassword);
		passwordField.setBounds(69, 33, 145, 20);

		contentPanel.add(passwordField);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							validatFields();
						} catch (DAOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void validatFields() throws DAOException {
		UsernamSrv userSrv = new UsernamSrv(DBUtil.getConnection());
		String miss = "Missing data of :" + "\n";
		if (usernameTextField.getText().isEmpty()
				|| passwordField.getText().isEmpty()) {

			if (usernameTextField.getText().isEmpty()) {
				miss += "Username.\n";
			}
			if (passwordField.getText().isEmpty()) {
				miss += "Password.\n";
			}

			JOptionPane.showMessageDialog(null, miss, null,
					JOptionPane.ERROR_MESSAGE);
		} else 
            
            if(userSrv.login(usernameTextField.getText(), passwordField.getText())){
            	
            	UserInfo.getInstance().setUsername(userSrv.getUser());
            	            	dispose();
            	new MainFrame().setVisible(true);
                
	}else{
            	JOptionPane.showMessageDialog(null, "Invalid user name or password.", null,
    					JOptionPane.ERROR_MESSAGE);
            
            }
	}
	

}
